<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://blog.gougucms.com
 */

declare (strict_types = 1);

namespace app\home\controller;

use app\home\BaseController;
use app\model\Action as ActionModel;
use app\model\Article as ArticleModel;
use app\model\Keywords as KeywordsModel;
use think\facade\Db;
use think\facade\View;

class Index extends BaseController
{
    public function index()
    {
        $slide = Db::name('SlideInfo')
            ->alias('s')
            ->join('file f', 's.img=f.id', 'LEFT')
            ->join('slide sl', 's.slide_id=sl.id', 'LEFT')
            ->field('s.id,s.title,s.src,f.filepath')
            ->where(['s.status' => 1, 'sl.name' => 'INDEX_SLIDE'])
            ->select();
        $links = Db::name('Links')->order('sort desc')->select();

		$book = Db::name('Book')->where(['status' => 1,'is_index'=>1])->order('sort desc')->select();
        $links = Db::name('Links')->order('sort desc')->select();
		$hot_articles = ArticleModel::where(['status' => 1])->order('read desc')->limit(10)->select();
        //博客统计信息
        $blog = get_system_config('blog');
        $startdate = strtotime($blog['create_time']);
        $nowtime = date('Y-m-d', time());
        $enddate = strtotime($nowtime);
        $counts = [];
        $counts['days'] = round(($enddate - $startdate) / 3600 / 24);
        $counts['title'] = $blog['title'];
        $counts['avatar'] = $blog['avatar'];
        $counts['article'] = ArticleModel::where(['status' => 1])->count();
        $counts['action'] = Db::name('UserLog')->count();
		$counts['download'] = Db::name('UserLog')->where(array('type'=>'down'))->count();
        View::assign([
            'seo' => get_system_config('web'),
            'search_keywords' => (new KeywordsModel())->search_key(),
            'hot_tag' => (new KeywordsModel())->tags(15),
            'article' => (new ArticleModel())->list(15),
            'cates' => (new ArticleModel())->cates(),
            'tags' => (new KeywordsModel())->tags(20, 1),
            'action' => (new ActionModel())->list(7),
            'hot_articles' => $hot_articles,
            'slide' => $slide,
            'book' => $book,
            'links' => $links,
            'counts' => $counts,
            'version' => CMS_VERSION,
        ]);
        add_user_log('view', '首页');
        return View();
    }

    public function logs()
    {
        add_user_log('view', '开发日志');
		$seo = get_system_config('web');
		$seo['title'] = '开发日志—勾股博客';
		View::assign('seo',$seo);
        return View('');
    }

	public function down()
    {
        $version = CMS_VERSION;
        add_user_log('down', $version.'版本代码');
        header("Location: https://blog.gougucms.com/storage/gougublog_".$version."_full.zip");
        //确保重定向后，后续代码不会被执行
        exit;
    }

    /*
     *分类文章
     */
    public function cate()
    {
        $seo = get_system_config('web');
        $seo['cate_id'] = 0;
        $param = get_params();
        if (!empty($param['id']) && $param['id'] > 0) {
            $seo['cate_id'] = $param['id'];
        }
        $list = (new ArticleModel())->page_list($param);
        // 获取分页显示
        $page = $list->render();
        $seo['action'] = 'cate';
        if ($seo['cate_id'] > 0) {
            $cate = Db::name('ArticleCate')->where('id', $seo['cate_id'])->find();
            $seo['title'] = $cate['title'].'分类的文章—勾股博客';
            $seo['keywords'] = $cate['keywords'];
            $seo['desc'] = $cate['desc'];
        }
        View::assign([
            'seo' => $seo,
            'article' => $list,
            'page' => $page,
            'cates' => (new ArticleModel())->cates(),
            'tags' => (new KeywordsModel())->tags(20, 1),
            'hot_articles' => (new ArticleModel())->list(10, 1),
        ]);
        add_user_log('view', $seo['title'] . '文章列表页');
        return View();
    }

    /*
     *标签文章
     */
    public function tags()
    {
        $param = get_params();
        $list = (new KeywordsModel())->article_list($param);
        // 获取分页显示
        $page = $list->render();

        $seo = [];
        $seo['cate_id'] = 0;
        $seo['action'] = 'tags';
        $seo['keywords'] = KeywordsModel::where('id', $param['id'])->value('title');
        $seo['title'] = $seo['keywords']. '关联的文章';
        $seo['desc'] = $seo['title'] . '关联的文章';

        View::assign([
            'seo' => $seo,
            'article' => $list,
            'page' => $page,
            'cates' => (new ArticleModel())->cates(),
            'tags' => (new KeywordsModel())->tags(20, 1),
            'hot_articles' => (new ArticleModel())->list(10, 1),
        ]);
        add_user_log('view', $seo['desc']);
        return View('cate');
    }

    /*
     *文章搜索
     */
    public function search()
    {
        $param = get_params();
        $list = (new ArticleModel())->page_list($param);
        // 获取分页显示
        $page = $list->render();

        $seo = [];
        $seo['cate_id'] = 0;
        $seo['action'] = 'search';
        $seo['keywords'] = $param['k'];
        $seo['title'] = $seo['keywords']. '关联的文章';
        $seo['desc'] = $seo['title'] . '关联的文章';

        View::assign([
            'seo' => $seo,
            'article' => $list,
            'page' => $page,
            'cates' => (new ArticleModel())->cates(),
            'tags' => (new KeywordsModel())->tags(20, 1),
            'hot_articles' => (new ArticleModel())->list(10, 1),
        ]);
        add_user_log('search', $seo['desc']);
        return View('cate');
    }

    /*
     *文章详情
     */
    public function detail()
    {
        $param = get_params();
        $article = (new ArticleModel())->detail($param['id']);
        ArticleModel::where('id', $param['id'])->inc('read')->update();

        $seo = [];
        $seo['cate_title'] = 0;
        $seo['keywords'] = $article['keyword_names'];
        $seo['title'] = $article['title'];
        $seo['desc'] = $article['desc'];

        View::assign([
            'seo' => $seo,
            'detail' => $article,
            'cates' => (new ArticleModel())->cates(),
            'tags' => (new KeywordsModel())->tags(20, 1),
            'hot_articles' => (new ArticleModel())->list(10, 1),
        ]);
        add_user_log('view', $seo['title'] . '文章详情', $param['id']);
        return View();
    }

    /*
     *动态
     */
    public function action()
    {
        $param = get_params();
        $param['limit'] = 30;
        $list = (new ActionModel())->page_list($param);
        // 获取分页显示
        $page = $list->render();
		$seo = get_system_config('blog');
        View::assign([
            'seo' => $seo,
            'list' => $list,
            'page' => $page,
            'cates' => (new ArticleModel())->cates(),
            'tags' => (new KeywordsModel())->tags(20, 1),
            'hot_articles' => (new ArticleModel())->list(10, 1),
        ]);
        add_user_log('view', '动态列表页');
        return View();
    }

    /*
     *文章归档
     */
    public function archives()
    {
        $tag_count = KeywordsModel::count();
        $article_count = ArticleModel::where(['status' => 1])->count();
        $cate_count = Db::name('ArticleCate')->count();
        $view_count = Db::name('UserLog')->count();

        $article = Db::name('Article')->where(['status' => 1])->field('id,title,create_time')->order('id desc')->select()->toArray();
        $date_archives = date_document($article);
        $month_archives = month_document($article);
        $year_archives = year_document($month_archives);
		$seo = get_system_config('web');
		$seo['title'] = '文章归档—勾股博客';
        View::assign([
            'seo' => $seo,
            'cate_count' => $cate_count,
            'tag_count' => $tag_count,
            'article_count' => $article_count,
            'view_count' => $view_count,
            'year_archives' => $year_archives,
            'date_archives' => json_encode($date_archives),
        ]);
        add_user_log('view', '文章归档页');
        return View();
    }
}
